package cn.sz.dzy.service;

import cn.sz.dzy.entity.Stock;
import cn.sz.dzy.mapper.StockMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description:
 * @author: DingZhenYun
 * @create: 2025-09-19 13:43
 **/
@Service
public class StockServiceImpl implements IStockService {

    @Resource
    private StockMapper stockMapper;

    @Override
    public synchronized void checkAndLock() {
        // 先查询库存是否充足
        Stock stock = this.stockMapper.selectById(1L);
        // 再减库存
        if (stock != null && stock.getCount() > 0) {
            stock.setCount(stock.getCount() - 1);
            this.stockMapper.updateById(stock);
        }
    }
}
